//最长湍流子数组
//测试链接：https://leetcode.cn/problems/longest-turbulent-subarray/
public class MaxTurbulenceSize {
    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        int[] f = new int[n];
        int[] g = new int[n];
        for (int i = 0; i < n; i++)
            f[i] = g[i] = 1;
        int ret = 1;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] < arr[i])
                f[i] = g[i - 1] + 1;
            else if (arr[i - 1] > arr[i])
                g[i] = f[i - 1] + 1;
            ret = Math.max(ret, Math.max(f[i], g[i]));
        }
        return ret;
    }
}
